#ifndef __ECHOSERVER_H__
#define __ECHOSERVER_H__
#include "ThreadPool.h"
#include "TcpConnection.h"
#include "TcpServer.h"
#include "Timer.h"
#include "Task.h"
#include "LoadBalanceStrategy.h"
#include <chrono>
#include <memory>
#include <random>
#include <sstream>

using std::ostringstream;

using TcpConnectionPtr = shared_ptr<TcpConnection>;
class MyTask
{
public:
    MyTask(const string &msg, const TcpConnectionPtr &con)
        : _msg(msg), _con(con) {}
    void process()
    {
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_int_distribution<int> dist(1000, 1000000);

        int number = dist(gen);

        auto start_time = std::chrono::high_resolution_clock::now();

        bool is_prime = true;
        if (number <= 1)
        {
            is_prime = false;
        }
        else if (number <= 3)
        {
            is_prime = true;
        }
        else if (number % 2 == 0 || number % 3 == 0)
        {
            is_prime = false;
        }
        else
        {
            for (int i = 5; i * i <= number; i += 6)
            {
                if (number % i == 0 || number % (i + 2) == 0)
                {
                    is_prime = false;
                    break;
                }
            }
        }

        auto end_time = std::chrono::high_resolution_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);

        ostringstream oss;
        string isprimestr = is_prime ? "素数\n" : "合数\n";
        oss << "数字 " << number << " 是" << isprimestr;
        oss << "判断耗时: " << duration.count() << " μs" << std::endl;
        oss << "----------------------------------------" << std::endl;
        _con->sendInLoop(oss.str());
    }

private:
    string _msg;
    TcpConnectionPtr _con;
};

class EchoServer
{
public:
    EchoServer(const string &ip, unsigned short port, int subReactorNum = 2, int threadNum = 4, int queueSize = 1024);
    ~EchoServer();
    void start();
    void stop();
    void onNewConnection(const TcpConnectionPtr &con);
    void onMessage(const TcpConnectionPtr &con);
    void onClose(const TcpConnectionPtr &con);

    // 定时器接口
    int64_t runAfter(std::chrono::milliseconds delay, TimerCallback &&callback);
    int64_t runEvery(std::chrono::milliseconds interval, TimerCallback &&callback);
    void cancelTimer(int64_t timerId);

    // 任务管理接口
    void initializeTasks();
    TimerTask *getTaskManager() { return _taskManager.get(); }

    // 辅助方法
    string getCurrentTime();
    void handleDelayCommand(const TcpConnectionPtr &con, const string &msg);
    
    // 负载均衡接口
    void setLoadBalanceStrategy(LoadBalanceStrategy strategy);
    LoadBalanceStrategy getLoadBalanceStrategy() const;
    std::pair<size_t, size_t> getConnectionRange();
    
    // 连接数管理
    void setMaxConnections(size_t maxConns);
    size_t getMaxConnections() const;

private:
    ThreadPool _threadPool;
    TcpServer _server;
    std::unique_ptr<TimerTask> _taskManager;
};

#endif
